package com.wwz.leetcode;

import org.junit.Test;

public class LeetCode28 {

    public int strStr(String haystack, String needle) {
        int hlength = haystack.length();
        int nlength = needle.length();
        // 为什么 i <= hlength - nlength  因为 最后几个还不匹配 则一定不会匹配成功  而且防止i+j 数组越界
        for (int i = 0; i <= hlength - nlength; i++) {
            boolean flag = true;
            for (int j = 0; j < nlength; j++) {
                if (haystack.charAt(i + j) != needle.charAt(j)) {
                    flag = false;
                }
            }
            if (flag) {
                return i;
            }
        }
        return -1;
    }

    @Test
    public void test() {
        System.out.println(strStr("aaaaa", "bba"));
        // 预期4
//        System.out.println(strStr("mississippi", "issip"));
//        System.out.println(strStr("hello", "ll"));


    }
}
